package com.lz.library.adapter

import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 *  author: you : 2020/4/25
 */
abstract class BindingAdapter<T>(initData: List<T>? = null) : com.lz.library.adapter.BaseAdapter<T, BindingViewHolder>(initData) {

    override fun onCreateItem(parent: ViewGroup, viewType: Int): BindingViewHolder =
        BindingViewHolder(DataBindingUtil.inflate<ViewDataBinding>(layoutInflater, getLayoutResId(viewType), parent, false).root)

    override fun onBindViewHolder(vh: BindingViewHolder, position: Int) {
        vh.getBinding<ViewDataBinding>().setVariable(getVariableId(vh.itemViewType), getItem(position))
    }

    override fun onViewRecycled(vh: BindingViewHolder) = vh.getBinding<ViewDataBinding>().unbind()

    @LayoutRes
    protected abstract fun getLayoutResId(viewType: Int): Int

    protected abstract fun getVariableId(viewType: Int): Int
}